package com.wuliu.driver.config;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.wuliu.driver.util.Code;
import com.wuliu.driver.util.Result;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class QuanXianBuZuHandler implements AccessDeniedHandler {


    /**
     *
     *
     * @param httpServletRequest  请求对象
     * @param httpServletResponse  响应对象
     * @param e  权限不足的异常
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        System.out.println("权限不足");
        // 告知我们给前端响应的是JSON格式，编码格式是UTF-8
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        //Result r = new Result(Code.Access_FAIL,"权限不足");

//        // 如何把对象转成json格式的字符串
//        ObjectMapper om = new ObjectMapper();
//        String s = om.writeValueAsString(r);
//        httpServletResponse.getWriter().write(s);
//
//        System.out.println(s);
        e.printStackTrace();// 权限不足的异常要打印
    }
}
